home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Text⁄Files / MakeWrite / MakeWrite Folder / MWMSpecOps.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-16  |  4.9 KB  |  270 lines  |  [TEXT/KAHL]

  1. # include    "MWMapInfo.h"
  2. # include    "MakeWrite.h"
  3.  
  4.  
  5. /*
  6.  * Initialize a MapSpec by allocating a mark string for it.
  7.  */
  8.  
  9. void
  10. InitMSpec (MapSpec *mSpec)
  11. {
  12.     mSpec->mark = (StringHandle) NewHandle ((Size) maxMarkLen + 1);
  13. }
  14.  
  15.  
  16. /*
  17.  * Terminate a MapSpec by tossing its mark string.  The procedure
  18.  * name is somewhat of a misnomer since it doesn't actually dispose
  19.  * of the MapSpec itself.
  20.  */
  21.  
  22. void
  23. TermMSpec (MapSpec *mSpec)
  24. {
  25.     DisposeHandle ((Handle) mSpec->mark);
  26. }
  27.  
  28.  
  29. /*
  30.  * Copy one init'ed MapSpec to another.  The most difficult part
  31.  * is making sure the mark string gets copied correctly.
  32.  */
  33.  
  34. void
  35. CopyMSpec (MapSpec *srcMSpec, MapSpec *dstMSpec)
  36. {
  37. StringHandle    srcStr, dstStr;
  38.  
  39.     dstStr = dstMSpec->mark;        /* save, since next line clobbers it */
  40.     *dstMSpec = *srcMSpec;
  41.     dstMSpec->mark = dstStr;        /* fix this field and copy the handle */
  42.     srcStr = srcMSpec->mark;
  43.     HLock ((Handle) srcStr);
  44.     HLock ((Handle) dstStr);
  45.     CopyString (*srcStr, *dstStr);
  46.     HUnlock ((Handle) srcStr);
  47.     HUnlock ((Handle) dstStr);
  48.     /*SetHandleSize (dstStr, (Size) 0);
  49.     HandAndHand (srcMSpec->mark, dstStr);*/
  50. }
  51.  
  52.  
  53. /*
  54.  * Zero the contents of an init'ed MapSpec.
  55.  */
  56.  
  57. void
  58. ClearMSpec (MapSpec *mSpec)
  59. {
  60.     mSpec->font = sameFont;
  61.     mSpec->size = sameSize;
  62.     mSpec->style = sameStyle;
  63.     (*mSpec->mark)[0] = 0;
  64.     mSpec->selStart = 0;
  65.     mSpec->selEnd = 0;
  66. }
  67.  
  68.  
  69. /*
  70.  * Compare two markers for equality.
  71.  */
  72.  
  73. short
  74. CompareMark (StringHandle s1, StringHandle s2)
  75. {
  76. short        result;
  77.  
  78.     HLock ((Handle) s1);
  79.     HLock ((Handle) s2);
  80.     result = CompareString (*s1, *s2);
  81.     HUnlock ((Handle) s1);
  82.     HUnlock ((Handle) s2);
  83.     return (result);
  84. }
  85.  
  86.  
  87. /*
  88.  * Compare two map specifications.
  89.  * Return:
  90.  *    0        m1 = m2
  91.  *    < 0        m1 < m2
  92.  *    > 0        m1 > m2
  93.  *
  94.  * Mark sorts before font, which sorts before size, which sorts
  95.  * before style.
  96.  *
  97.  * For all of font, size and style, the "same" selection is greater,
  98.  * so that generic selections sort to the end of the list.  Font
  99.  * comparisons are otherwise based on the name of the font, rather
  100.  * than its number.   A string compare is avoided by keeping the
  101.  * fonts in alphabetic order in the font information structures.
  102.  *
  103.  * Note that the style comparison does not use the manifest constants
  104.  * defined for each style value, and so would break if style coding
  105.  * changed!
  106.  */
  107.  
  108. short
  109. CompareMSpec (MapSpec *m1, MapSpec *m2)
  110. {
  111. short    i;
  112. short    s1, s2;
  113.  
  114.     if ((i = CompareMark (m1->mark, m2->mark)) != 0)
  115.         return (i);
  116.  
  117.     if (m1->font != m2->font)
  118.     {
  119.         if (m1->font == sameFont)
  120.             return (1);
  121.         if (m2->font == sameFont)
  122.             return (-1);
  123.         return (FontIndex (m1->font) - FontIndex (m2->font));
  124.     }
  125.  
  126.     if ((i = m1->size - m2->size) != 0)
  127.     {
  128.         if (m1->size == sameSize)
  129.             return (1);
  130.         if (m2->size == sameSize)
  131.             return (-1);
  132.         return (i);
  133.     }
  134.  
  135.     s1 = m1->style;
  136.     s2 = m2->style;
  137.     if (s1 - s2 != 0)
  138.     {
  139.         if (s1 == sameStyle)
  140.             return (1);
  141.         if (s2 == sameStyle)
  142.             return (-1);
  143.         for (i = 0; i < 7; ++i)
  144.         {
  145.             if (s1 == 0)
  146.             {
  147.                 if (s2 != 0)
  148.                     return (-1);
  149.             }
  150.             else if (s2 == 0)
  151.             {
  152.                 return (1);
  153.             }
  154.             else if (s1 & 1)
  155.             {
  156.                 if (!(s2 & 1))
  157.                     return (-1);
  158.             }
  159.             else if (s2 & 1)
  160.                 return (1);
  161.             s1 >>= 1;
  162.             s2 >>= 1;
  163.         }
  164.     }
  165.     return (0);            /* equal */
  166. }
  167.  
  168.  
  169. /*
  170.  * Convert map specs to map text.  Do this by converting input specs
  171.  * and output specs.
  172.  */
  173.  
  174. void
  175. MSpecToMStr (MapSpec *mSpec, MapStr *mStr)
  176. {
  177.     HLock ((Handle) mSpec->mark);
  178.     CopyString (*mSpec->mark, mStr->markStr);
  179.     HUnlock ((Handle) mSpec->mark);
  180.     FontToStr (mSpec->font, mStr->fontStr);
  181.     SizeToStr (mSpec->size, mStr->sizeStr);
  182.     StyleToStr (mSpec->style, mStr->styleStr);
  183. }
  184.  
  185.  
  186. /*
  187.  * Convert font number to font name.  The font *must* be legal.
  188.  */
  189.  
  190. void
  191. FontToStr (short font, StringPtr str)
  192. {
  193.     FontName (FontIndex (font), str);
  194. }
  195.  
  196.  
  197. /*
  198.  * Convert size value to string.
  199.  */
  200.  
  201. void
  202. SizeToStr (short size, StringPtr str)
  203. {
  204.     if (size == sameSize)        /* no size specified, use default */
  205.         CopyString ("\pSame", str);
  206.     else
  207.         NumToString ((long) size, str);
  208. }
  209.  
  210.  
  211. /*
  212.  * Convert style value to string. If the style has only one attribute
  213.  * bit set, use the attribute name, otherwise use abbreviated form with
  214.  * one letter for each attribute.
  215.  */
  216.  
  217. static StringPtr    styleStr[7] =
  218. {
  219.     "\pBold",
  220.     "\pItalic",
  221.     "\pUnder",
  222.     "\pOutline",
  223.     "\pShadow",
  224.     "\pHigh",
  225.     "\pLow"
  226. };
  227.  
  228.  
  229. void
  230. StyleToStr (short style, StringPtr str)
  231. {
  232. short    i, style2;
  233.  
  234.     if (style == sameStyle)        /* no style specified, use default */
  235.         CopyString ("\pSame", str);
  236.     else if (style == 0)            /* no bits set = plain text */
  237.         CopyString ("\pPlain", str);
  238.     else
  239.     {
  240.         style2 = style;
  241.         for (i = 0; i < 7; ++i)
  242.         {
  243.             if ((style2 & 1))
  244.             {
  245.                 if (style2 != 1)
  246.                     break;            /* more than 1 bit set */
  247.                 CopyString (styleStr[i], str);
  248.                 return;
  249.             }
  250.             style2 >>= 1;
  251.         }
  252.         i = 0;
  253.         if (style & styleBold)
  254.             str[++i] = 'B';
  255.         if (style & styleItalic)
  256.             str[++i] = 'I';
  257.         if (style & styleUnder)
  258.             str[++i] = 'U';
  259.         if (style & styleOutline)
  260.             str[++i] = 'O';
  261.         if (style & styleShadow)
  262.             str[++i] = 'S';
  263.         if (style & styleSuper)
  264.             str[++i] = 'H';
  265.         if (style & styleSub)
  266.             str[++i] = 'L';
  267.         str[0] = i;
  268.     }
  269. }
  270.